/**
 * 
 */
package com.study.echart;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Administrator
 * @date 2019年7月9日
 */
@Slf4j
public class GenerateEchart {

	public static void main(String[] args) {
		generateEChart(TestEchart.testEchart(), "demo.png");
	}
		
	public static String generateEChart(String options, String imgName) {
		//String dataPath = writeFile(options);
	    String path = "d:\\logs\\" + imgName;
	    try {
	        File file = new File(path);     //文件路径（路径+文件名）
	        if (!file.exists()) {   //文件不存在则创建文件，先创建目录
	            File dir = new File(file.getParent());
	            dir.mkdirs();
	            file.createNewFile();
	        }
	        //echarts-convert.js 脚本所在 path
	        String JSpath = "D:\\06java\\workspace\\study-demo\\src\\main\\java\\com\\study\\echart\\echarts-convert.js";
	        log.info("start cmd exec generate png");

	        String cmd = "phantomjs " + JSpath + " -options " + options + " -outfile " + path + " -width 1680 -height 500";
	        Process process = Runtime.getRuntime().exec(cmd);
	        System.out.println(cmd);
	        log.info("end cmd exec generate png");
	        BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
	        String line = "";
	        while ((line = input.readLine()) != null) {
	        	log.info(line);
	        }
	        input.close();
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        return path;
	    }
	}
}
